package com.gameone;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
	private SurfaceHolder holder;
	private GameLoopThread gameLoopThread;
	// private List<Sprite> sprites = new ArrayList<Sprite>();
	private Sprite1 sprites1 = new Sprite1();
	private Bitmap bmp1;
	private Sprite sprites = new Sprite();
	private Bitmap bmp;
	private Rect spriteBounds = new Rect(0, 0, 0, 0);
	private Rect sprite1Bounds = new Rect(0, 0, 0, 0);
	private Point sprite1;
	private Point sprite;
	private boolean collisionDetected = false;

	public GameView(Context context) {
		super(context);
		gameLoopThread = new GameLoopThread(this);
		holder = getHolder();
		holder.addCallback(new SurfaceHolder.Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				boolean retry = true;
				gameLoopThread.setRunning(false);
				while (retry) {
					try {
						gameLoopThread.join();
						retry = false;
					} catch (InterruptedException e) {
					}
				}checkForCollision();
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				createSprites();
				gameLoopThread.setRunning(true);
				gameLoopThread.start();

			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
			}
		});

	}

	private void createSprites() {
		/*
		 * sprites.add(createSprite(R.drawable.ball1));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites.add(createSprite(R.drawable.ball3));
		 * sprites1.add(createSprite(R.drawable.racket2));
		 */
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ball3);
		sprites = new Sprite(this, bmp);
		spriteBounds = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
		bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.racket2);
		sprites1 = new Sprite1(this, bmp1);
		sprite1Bounds = new Rect(0, 0, bmp1.getWidth(), bmp1.getHeight());
	}

	private boolean checkForCollision() {

		Rect r1 = new Rect(sprite1.x, sprite1.y, sprite1.x
				+ sprite1Bounds.width(), sprite1.y + sprite1Bounds.height());
		Rect r = new Rect(sprite.x, sprite.y, sprite.x + spriteBounds.width(),
				sprite.y + spriteBounds.height());

		if (r1.intersect(r)) 
		collisionDetected = true;
		return collisionDetected;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		sprites.onDraw(canvas);
		sprites1.onDraw(canvas);
		
	}
}

/*
 * import android.content.Context; import android.graphics.Bitmap; import
 * android.graphics.BitmapFactory; import android.graphics.Canvas; import
 * android.graphics.Color; import android.view.SurfaceHolder; import
 * android.view.SurfaceView;
 * 
 * public class GameView extends SurfaceView { private Bitmap bmp,bmp1; private
 * SurfaceHolder holder; private GameLoopThread gameLoopThread; private Sprite
 * sprite,sprite1;
 * 
 * public GameView(Context context) { super(context); gameLoopThread = new
 * GameLoopThread(this); holder = getHolder(); holder.addCallback(new
 * SurfaceHolder.Callback() {
 * 
 * @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry
 * = true; gameLoopThread.setRunning(false); while (retry) { try {
 * gameLoopThread.join(); retry = false; } catch (InterruptedException e) { } }
 * }
 * 
 * @Override public void surfaceCreated(SurfaceHolder holder) {
 * gameLoopThread.setRunning(true); gameLoopThread.start(); }
 * 
 * @Override public void surfaceChanged(SurfaceHolder holder, int format, int
 * width, int height) { } }); bmp = BitmapFactory.decodeResource(getResources(),
 * R.drawable.ball3); sprite = new Sprite(this,bmp); bmp1 =
 * BitmapFactory.decodeResource(getResources(), R.drawable.ball2); sprite1 = new
 * Sprite(this,bmp1); }
 * 
 * @Override protected void onDraw(Canvas canvas) {
 * canvas.drawColor(Color.BLACK); sprite.onDraw(canvas); sprite1.onDraw(canvas);
 * } }
 * 
 * import android.content.Context; import android.graphics.Bitmap; import
 * android.graphics.BitmapFactory; import android.graphics.Canvas; import
 * android.graphics.Color; import android.view.SurfaceHolder; import
 * android.view.SurfaceView;
 * 
 * public class GameView extends SurfaceView { private Bitmap bmp,bmp1; private
 * SurfaceHolder holder; private GameLoopThread gameLoopThread; private int x =
 * 10;//(getWidth()/2); private int xSpeed = 25; private int ySpeed = 20;
 * private int y = 10; private int x1 = 200; private int y1 = 100; private int
 * xSpeed1 = 1; private int ySpeed1 = 1;
 * 
 * 
 * 
 * 
 * public GameView(Context context) { super(context); gameLoopThread = new
 * GameLoopThread(this); holder = getHolder(); holder.addCallback(new
 * SurfaceHolder.Callback() {
 * 
 * @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry
 * = true; gameLoopThread.setRunning(false); while (retry) { try {
 * gameLoopThread.join(); retry = false; } catch (InterruptedException e) { } }
 * }
 * 
 * @Override public void surfaceCreated(SurfaceHolder holder) {
 * gameLoopThread.setRunning(true); gameLoopThread.start(); }
 * 
 * @Override public void surfaceChanged(SurfaceHolder holder, int format, int
 * width, int height) { } }); bmp = BitmapFactory.decodeResource(getResources(),
 * R.drawable.ball2); bmp1 = BitmapFactory.decodeResource(getResources(),
 * R.drawable.ball3); }
 * 
 * 
 * 
 * @Override protected void onDraw(Canvas canvas) { if (x + xSpeed < 0) xSpeed =
 * 10; if (x + xSpeed > getWidth() - bmp.getWidth()) xSpeed = -10; if (y +
 * ySpeed < 0) ySpeed = 10; if (y + ySpeed > getHeight() - bmp.getHeight())
 * ySpeed = -10; x = x + xSpeed; y = y + ySpeed;
 * 
 * if (x1 + xSpeed1 < 0) xSpeed = 10; if (x1 + xSpeed1 > getWidth() -
 * bmp1.getWidth()) xSpeed = -10; if (y1 + ySpeed1 < 0) ySpeed = 10; if (y1 +
 * ySpeed1 > getHeight() - bmp1.getHeight()) ySpeed = -10; x1 = x1 + xSpeed1; y1
 * = y1 + ySpeed1; if (x == getWidth() - bmp.getWidth()) { xSpeed = -10; y = y +
 * 5; } if (x == 0) { xSpeed = 10; y = y + 5; } x = x + xSpeed; if (y ==
 * getHeight() - bmp.getHeight()) { ySpeed = -10; } if (y == 0) { ySpeed = 10; }
 * y = y + ySpeed; canvas.drawColor(Color.WHITE); canvas.drawBitmap(bmp, x , y,
 * null); canvas.drawBitmap(bmp1, x1 , y1, null); }
 */

